// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        sp_target:cc.Sprite,

        fm_cocos:cc.SpriteFrame,
        fm_player:cc.SpriteFrame,

        nd_tips:cc.Node,
    },

    onLoad(){
        if(cc.sys.platform === cc.sys.WECHAT_GAME){
            this.nd_tips.active = true;
            return;
        }else{
            this.nd_tips.active = false;
        }

        let rt = new cc.RenderTexture();
        rt.initWithSize(640 + 90,640 + 90);

        /**
         *  左上角为原点
         */
        // https://forum.cocos.org/t/rendertexture/73799/5
        // 在微信上,开启 CLEANUP_IMAGE_CACHE 的时候, drawTextureAt这个api用不了.
        // 有种方式是 使用 一个camera去渲染
        //
        rt.drawTextureAt(this.fm_cocos._texture,90,90);

        this.rt = rt;
        this.frame = new cc.SpriteFrame(rt);

    },

    start () {


    },

    update (dt) {
        let rt = this.rt;
        for(let i = 0; i < 10; ++i){
            let x = Math.random() * 640;
            let y = Math.random() * 640;
            rt.drawTextureAt(this.fm_player._texture,x,y);
        }
        this.frame._refreshTexture(rt);
        this.sp_target.spriteFrame = this.frame;
    },

    back_btnClicked(){
        cc.director.loadScene('entry/mainScene');
    },
});
